package com.googlecode.gwtbb.client.only.widget.basic;

import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.*;
import com.googlecode.gwtbb.client.only.glue.TokenGenerator;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Abraham Grief
 * @version $Id$
 * @since Jul 30, 2008
 */
public class HistoryTabBar extends Composite implements SourcesTabEvents {
	TabBar tabBar = new TabBar() {
		@Override
		public void onClick(Widget sender) {
			TokenGenerator tokenGenerator = tokenGenerators.get(sender);
			if (tokenGenerator != null)
				History.newItem(tokenGenerator.generateToken());
		}
	};
	private Map<Widget, TokenGenerator> tokenGenerators = new HashMap<Widget, TokenGenerator>();

	public HistoryTabBar() {
		initWidget(tabBar);
	}

	public void addTabListener(TabListener listener) {
		tabBar.addTabListener(listener);
	}

	public void insertTab(Widget widget, int beforeIndex, TokenGenerator tokenGenerator) {
		tokenGenerators.put(widget, tokenGenerator);
		tabBar.insertTab(widget, beforeIndex);
	}

	public void removeTab(int index) {
		// todo also remove from tokenGenerators
		tabBar.removeTab(index);
	}

	public void removeTabListener(TabListener listener) {
		tabBar.removeTabListener(listener);
	}

	public boolean selectTab(int index) {
		return tabBar.selectTab(index);
	}
}
